//
//  CALAddNewFoodViewController.m
//  CalCounter
//
//  Created by LamLT on 10/09/2013.
//  Copyright (c) 2013 Le thanh Lam. All rights reserved.
//

#import "CALAddNewFoodViewController.h"
#import "NSString+Ex.h"
#import "DataHelper.h"


@interface CALAddNewFoodViewController ()

@end

@implementation CALAddNewFoodViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withFoodName:(NSString *)_str
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self._foodName = _str;
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    txtFoodName.text = self._foodName;
    self.title = NSLocalizedString(@"newfood", nil);
    //scrview
    [scrView setContentSize:CGSizeMake(320, 500)];
    //add save button on navigation
    UIBarButtonItem *btnSave = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"save", nil) style:UIBarButtonItemStylePlain target:self action:@selector(btnSaveClicked:)];
    self.navigationItem.rightBarButtonItem = btnSave;

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [scrView setContentSize:CGSizeMake(320, 500)];
    [scrView setContentOffset:CGPointMake(0, 0)];
    if (textField == txtWeight) {
        [txtWeight resignFirstResponder];
    }
    if (textField == txtFoodName) {
        [txtFoodName resignFirstResponder];
    }
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (textField == txtWeight) {
//        [scrView setContentSize:CGSizeMake(320, 520)];
        [scrView setContentOffset:CGPointMake(0, 120)];
    }
    return YES;
}

- (void)btnSaveClicked:(id)sender{
    if ([self validateTxtField]) {
        CALFood *food = [[DataHelper sharedDatabase] getFoodWithName:txtFoodName.text];
        if (food) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:NSLocalizedString(@"FoodExist", nil) delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
        else{
            CALFood *newFood = [[CALFood alloc] init];
            newFood.foodName = txtFoodName.text;
            newFood.cals = [txtWeight.text floatValue];
            [[DataHelper sharedDatabase] addNewFood:newFood];
            if(self.delegate != nil && [self.delegate respondsToSelector:@selector(didAddNewFood:)]){
                [self.delegate didAddNewFood:newFood];
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    
}

- (BOOL)validateTxtField{

    txtWeight.text = [txtWeight.text stringByReplacingOccurrencesOfString:@"," withString:@"."];
    
    static NSString *floatRegex = @"^(?:[1-9]\\d*|0)?(?:\\.\\d+)?$";
    BOOL isValid = [txtWeight.text isMatchRegex:floatRegex];

    if ([txtWeight.text isEqualToString:@""] || [txtFoodName.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:NSLocalizedString(@"InputRequired", nil) delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];

        return NO;
    }
    else if (!isValid){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:NSLocalizedString(@"InvalidValue", nil) delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];

        return NO;
    }
    else if (![self limitedSymbol:txtWeight.text]){
        return NO;
    }
    else{
        return YES;
    }
}

- (BOOL)limitedSymbol:(NSString *)str{
    if ([str floatValue] > 9999) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:NSLocalizedString(@"BigValue", nil) delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        return NO;
    }
    else{
        return YES;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
